package com.yxuan.springboot.service.impl;

import com.yxuan.springboot.mapper.EnrollmentPlanMapper;
import com.yxuan.springboot.mapper.ProfessionalScoreMapper;
import com.yxuan.springboot.mapper.SchoolScoreMapper;
import com.yxuan.springboot.model.EnrollmentPlan;
import com.yxuan.springboot.model.ProfessionalScore;
import com.yxuan.springboot.model.SchoolPlans;
import com.yxuan.springboot.model.SchoolScore;
import com.yxuan.springboot.service.SchoolScoreService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


/**
 * 
 * @author zchao
 *    SchoolScore业务层实现类
 * @date 2023-06-05 19:58:20
 */
@Slf4j
@Service
@Transactional
public class SchoolScoreServiceImpl extends BaseServiceImpl<SchoolScore> implements SchoolScoreService {


    @Autowired
    private SchoolScoreMapper schoolScoreMapper;

    @Autowired
    private ProfessionalScoreMapper professionalScoreMapper;

    @Autowired
    private EnrollmentPlanMapper enrollmentPlanMapper;





    @Override
    public List<Integer> findAllAvailableSchoolsId(String lowest_order, String province) throws Exception {


        //   根据school_score表录取最低排名以及考生高考省份查询能上的所有学校school_id,即where 排名>=最低排名,这里应该加上科目
        List<Integer> allAvailableSchoolsId = schoolScoreMapper.findAllAvailableSchoolsId(lowest_order, province);


        return allAvailableSchoolsId;
    }

    @Override
    public Integer findAllAvailableNumber(String lowest_order, String province) throws Exception {

        Integer allAvailableNumber = schoolScoreMapper.findAllAvailableNumber(lowest_order, province);


        return allAvailableNumber;
    }

    @Override
    public List<SchoolPlans> findAlldatedSubject(String targetYear, String province, Integer schoolId) throws Exception {           //参数里要再加一个选科

        //根据checkNeed查询到对应的数据条数，就可以查询到年份，最低分，最低位次                                                                 //修改为根据选科查询历年来的最低分，最低位次


        List<SchoolScore> alldatedSubjects = schoolScoreMapper.findAlldatedSubject(targetYear, province, schoolId);                  //根据选科，school_id,省份，有查取到数据年份重复的问题，取最低分数中最高的那个
        List<SchoolPlans> schoolPlans =new ArrayList<>();
        //查到了有几个选科
        for (int i = 0; i < alldatedSubjects.size(); i++) {
            SchoolScore schoolScore = alldatedSubjects.get(i);

            Integer schoolId1 = schoolScore.getSchoolId();
            Double lowestScore = schoolScore.getLowestScore();
            Integer lowestOrder = schoolScore.getLowestOrder();
            //根据school_id，最低分，最低位次查询professional_score表里的科类，最低分，最低位次，省份（可能会是null,要加个非空判断）
            ProfessionalScore professionalScore = new ProfessionalScore();
            professionalScore.setSchoolId((long)schoolId1);
            professionalScore.setLowestScore(String.valueOf(lowestScore));
            professionalScore.setLowestOrder(String.valueOf(lowestOrder));
            ProfessionalScore targetProfessionalScore = professionalScoreMapper.queryObjectByPramas(professionalScore);

            if(targetProfessionalScore!=null){

                //根据科类，省份，school_id,年份查询到enrollment_plan表里所有该科类的录取数
                EnrollmentPlan enrollmentPlan = new EnrollmentPlan();
                enrollmentPlan.setSchoolId(targetProfessionalScore.getSchoolId());
                enrollmentPlan.setSpecialityClassification(targetProfessionalScore.getSpecialityClassification());
                enrollmentPlan.setProvinceName(targetProfessionalScore.getProvinceName());
                enrollmentPlan.setStudentYear(schoolScore.getScoreYear());                                       //这是查所有科类（文理科）的累计录取数                               //括号里加一个targetProfessionalScore.getProfessionalName()作为参数
                Integer requiredNumber = enrollmentPlanMapper.findRequiredNumber(schoolId,targetProfessionalScore.getSpecialityClassification(), targetProfessionalScore.getProvinceName(), schoolScore.getScoreYear());
                //对plan进行参数赋值
                SchoolPlans schoolPlans1 = new SchoolPlans();
                schoolPlans1.setYear(schoolScore.getScoreYear());
                schoolPlans1.setLowerScore(schoolScore.getLowestScore());
                schoolPlans1.setLowerOrder(schoolScore.getLowestOrder());
                schoolPlans1.setType(targetProfessionalScore.getProfessionalName());
                schoolPlans1.setCheckNeed(schoolScore.getCheckNeed());
                schoolPlans1.setCount(requiredNumber);



                schoolPlans.add(schoolPlans1);
                if(schoolPlans.size()==3){
                    break;
                }
            }else {

                SchoolPlans schoolPlans1 = new SchoolPlans();
                schoolPlans.add(schoolPlans1);

            }






        }



        return schoolPlans;
    }
}
